<?php
namespace Admin\Controller;

class PlanStatsController extends Controller {

    public function index() {
        $m = D2('PlanStats');

        $currPageTotal = array();          //当前页汇总
        $allPageTotal  = array();          //所有页汇总
        $expr          = $this->indexExpr;

        $allPageTotalField = array(
            'SUM(view) AS view',
            'SUM(num) AS num',
            'SUM(deduction) AS deduction',
            'SUM(click) AS click',
            'SUM(do2_any_click) AS do2_any_click',
            'SUM(ader_pay) AS ader_pay',
            'SUM(siter_income) AS siter_income',
            'SUM(new_user) AS new_user',
            'SUM(reach) AS reach',
            'SUM(device_1) AS device_1',
            'SUM(device_2) AS device_2',
            'SUM(device_3) AS device_3',
            'SUM(device_4) AS device_4',
            'SUM(browser) AS browser',
            'SUM(r_searcher) AS r_searcher',
        );


        try {
          
        if($allPageTotal = $m->field($allPageTotalField)->where($expr)->find()) {
            $this->assign('allPageTotal', $allPageTotal);
        }

        /**
         * 获取分页数据并汇总当前页数据
         */
        if($page = $m->where($expr)->order($this->sortExpr)->page2()) {
            foreach($page['record'] as $v) {
                if(empty($currPageTotal)) {
                    $currPageTotal = $v;
                    unset($currPageTotal['plan_id'], $currPageTotal['plan_name'], $currPageTotal['plan_type']);
                } else {
                    foreach($currPageTotal as $k2 => $v2) {
                        if($k2 == 'day') {
                            continue;
                        }

                        if(in_array($k2, array('ader_pay', 'siter_income'))) {
                            $currPageTotal[$k2] = bcadd($currPageTotal[$k2], $v[$k2], C('money_scale'));
                        }  else {
                            $currPageTotal[$k2] += $v[$k2];
                        }
                    }
                }
            }

            $this->assign('currPageTotal', $currPageTotal);
        }
        } catch(\Exception $e) {
          
        }

        $this->assign('page', $page);
        $this->display();
    }

    /**
     * apiGetNumFlow
     * 按日期区域获取结算数据
     *
     * 返回结果结构：
     * array(
     *    '计划类型' => array('日期' => '结算数‘)
     * )
     *
     * @return void
     */
    public function apiGetNumFlow() {
        $day  = I('get.day',  null);
        $day2 = I('get.day2', null);

        if(empty($day) || empty($day2)) {
            showJson(array('s' => false, 'm' => '请指定完整的日期区间，必须指定两个日期'));
        }

        $a = strtotime($day);
        $b = strtotime($day2);

        if(!$a || !$b) {
            showJson(array('s' => false, 'm' => '请检查指定的日期格式是否正确'));
        }

        if($a > $b) {
            list($day, $day2) = array($day2, $day);
            list($a, $b)      = array($b, $a);
        }

        $m    = D2();
        $expr = array(
            'day' => array('between', array($day, $day2)),
        );
        $group = 'day, plan_type';
        $field = 'day, plan_type, SUM(num) AS num';

        if(!$r = $m->field($field)->where($expr)->group($group)->order('day ASC')->select()) {
            showJson(array('s' => false, 'm' => '未找到数据'));
        }

        $r2   = array();
        $days = array();

        foreach($r as $v) {
            $key    = $v['plan_type'];
            $key2   = $v['day'];
            $days[] = $key2;
            $r2[$key][$key2] = (int)$v['num'];
        }

        $r3 = array(
            'cate' => $days,
            'dao'  => $r2,
        );

        showJson(array('s' => true, 'd' => $r3));
    }

    /**
     * apiGetPlanListByDay
     * 按日期返回计划报表中的计划部分信息
     *
     * @return void
     */
    public function apiGetPlanListByDay() {
        $m   = D2('PlanStats');
        $day = I('get.day', null);

        if (empty($day)) {
            showJson(array('s' => false, 'm' => '请指定日期'));
        }

        $expr = array('day' => $day);

        if (!$records = $m->field('plan_id')->where($expr)->select()) {
            showJson(array('s' => false, 'm' => '没有找到计划报表数据'));
        }

        $planIds = array();

        foreach ($records as $k => $v) {
            $planIds[] = $v['plan_id'];
        }

        $planModel = D2('Plan');
        $field = array('planid AS plan_id', 'planname AS plan_name');
        $order = 'uid ASC, planid ASC';

        $r = ($d = $planModel->field($field)->where(array('planid' => array('IN', $planIds)))->order($order)->select())
             ? array('s' => true, 'd' => $d)
             : array('s' => false, 'm' => '没有找到计划数据');

        showJson($r);
    }

}
